package com.lianmai.chaincard.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lianmai.chaincard.R;
import com.lianmai.chaincard.bean.AreaInfo;

import java.util.List;

public class CityAdapter extends BaseQuickAdapter<AreaInfo, BaseViewHolder> {

    public CityAdapter(@Nullable List<AreaInfo> data) {
        super(data);
    }

    @Override
    protected int getDefItemViewType(int position) {
        AreaInfo areaInfo = getItem(position);
        if (areaInfo != null) {
            return areaInfo.type;
        } else {
            return 0;
        }
    }


    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new CityHolder(mLayoutInflater.inflate(R.layout.item_city, parent, false));
        } else {
            return new PinnedHolder(mLayoutInflater.inflate(R.layout.item_pinned_header, parent, false));
        }
    }

    @Override
    protected void convert(BaseViewHolder holder, AreaInfo item) {
        if (item != null) {
            if (holder instanceof CityHolder) {
                ((CityHolder) holder).city_name.setText(item.name);
                ((CityHolder) holder).city_num.setText(String.format("+%s", item.areacode));
                holder.addOnClickListener(R.id.ll_select_area);
            } else {
                String letter = item.pys.substring(0, 1);
                ((PinnedHolder) holder).city_tip.setText(letter);
            }
        }
    }

    public int getLetterPosition(String letter) {
        for (int i = 0; i < getData().size(); i++) {
            AreaInfo areaInfo = getData().get(i);
            if (areaInfo != null) {
                if (areaInfo.type == 1 && areaInfo.pys.equals(letter)) {
                    return i;
                }
            }
        }
        return -1;
    }

    class CityHolder extends BaseViewHolder {

        TextView city_name;
        TextView city_num;

        CityHolder(View view) {
            super(view);
            city_name = view.findViewById(R.id.tv_city_name);
            city_num = view.findViewById(R.id.tv_city_num);
        }
    }


    class PinnedHolder extends BaseViewHolder {

        TextView city_tip;

        PinnedHolder(View view) {
            super(view);
            city_tip = view.findViewById(R.id.tv_city_tip);
        }
    }
}
